
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/renderer/render/textrender.js
 * @Author: 
 * @Date: 2017-03-23T11:12:12+08:00
 * @LastEditors: 
 * @LastEditTime: 2021-08-17T15:19:57+08:00
 * Copyright (c) 2018-2022 
 */
                                                    
/**
 * Created by zhangyong on 2017/3/24.
 */

import GeometryRender from './geomertyrender'
import {Transform} from '../../data/matrix/transform'
import {colorToString} from '../../utils/helpers'
import drawPoint from './drawPoint'
// import TextStyle from '../../style/textstyle'
// import LineStyle from '../../style/linestyle'

export default class TextRender extends GeometryRender {
  
  constructor (context) {
    super(context)
  
    this._pixelCoordinates = []
  }
  
  /**
   *
   * @param feature
   * @param transform
   */
  render (feature, renderStyle, transform) {
    let textStyle = renderStyle[0].textStyle
    if (!feature.displayText || !textStyle) {
      return
    }
    const displayText = String(feature.displayText)
    textStyle.text = displayText

    const ctx = this.context
    ctx.save()

    let id;
    if(feature && feature.mark){
      id = feature.mark.id;
    }

    const geometry = feature.geometry
    const coordinates = geometry.getFlatInteriorPoint(id);
    
    this._pixelCoordinates = []
    const pixelCoordinates = Transform.transform2D(
      coordinates, 0, coordinates.length, 2,
      transform, this._pixelCoordinates)
    
    for (let i = 0, len = pixelCoordinates.length ;i < len ; i += 2) {
      if (i >= 2) {
        break;
      }

      const xValue = pixelCoordinates[i]
      const yValue = pixelCoordinates[i + 1]
      
      // set the context text style
      this._setTextStyle(ctx, textStyle)
  
      let offsetX = textStyle.offsetX - 10
      let offsetY = textStyle.offsetY - 10
      let stroke = textStyle.stroke ? true : false
      const fill = textStyle.fill ? true : false
      const text = textStyle.text

      if(feature.showPoint){
        //var offX = parseFloat(displayText.split('*')[0])*1.5
        var offX = 0;
        //textStyle._fill[0]=255;
        //textStyle._fill[2]=0;
        stroke=false;
	      offsetX = textStyle.offsetX - offX - ctx.measureText(textStyle.text).width/2;
	      offsetY = textStyle.offsetY -10;
      }
      let x = xValue + offsetX
      let y = yValue + offsetY
      let lines = text.split('\n')
      const numLines = lines.length
      let fontSize, lineY
      if (numLines > 1) {
        fontSize = Math.round(ctx.measureText('M').width * 1.5)
        lineY = y - (((numLines - 1) / 2) * fontSize)
      } else {
        fontSize = 0
        lineY = y
      }

      if (textStyle.background) {
        ctx.save()
        const width = ctx.measureText(text).width;
        ctx.fillStyle = textStyle.background
        ctx.fillRect(x, y - 10 , width, 18);
        ctx.restore();
      }

      for (let lineIndex = 0; lineIndex < numLines; lineIndex++) {
        const lineText = lines[lineIndex]
        if (stroke) {
          ctx.strokeText(lineText, x, lineY)
        }
        if (fill) {
          ctx.fillText(lineText, x, lineY)
        }
    
        lineY = lineY + fontSize
      }
    }
  
    ctx.restore()
  }
  
  /**
   *
   * @param ctx
   * @param textStyle
   * @private
   */
  _setTextStyle (ctx, textStyle) {
    const borderStyle = textStyle.stroke
  
    ctx.font = textStyle.font
    if (borderStyle) {
      ctx.strokeStyle = colorToString(borderStyle.color, 1)
      ctx.lineWidth = borderStyle.width
      ctx.lineCap = borderStyle.lineCap
      ctx.lineJoin = borderStyle.lineJion
    }
    ctx.fillStyle = colorToString(textStyle.fill, 1)
    ctx.textBaseline = textStyle.textBaseline
    ctx.textAlign = textStyle.textAlign
  }
  
}
